<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>更新时刻表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <script src="js/jquery-3.6.0.min.js"></script>

    <script>
        /* 初始化 */
        function initForm(form) {
            var data = JSON.parse(window.sessionStorage.getItem('user') || []);
            $("input[name='userId']").attr("value", data.userId)
            $("input[name='userName']").attr("value", data.userName)
            $("input[name='userMobile']").attr("value", data.userMobile)
            $("input[name='userSex'][title='男']").attr('checked', '男' === data.userSex);
            $("input[name='userSex'][title='女']").attr('checked', '女' === data.userSex);
            // 更新全部（如果不更新表单，checked不生效！）
            form.render();
            /* 清理session */
            window.sessionStorage.removeItem('user');
        }

        /* Ajax异步更新用户信息 */
        function update(data) {
            //请求参数
            var user = data.field;
            $.ajax({
                //请求方式
                type: 'POST',
                //请求的媒体类型
                contentType: 'application/json;charset=UTF-8',
                //请求地址
                url: 'rbsystemUser/update',
                //数据，json字符串
                data: JSON.stringify(user),
                // 请求数据类型
                dataType: 'JSON',
                //请求成功
                success: function (data) {
                    // 经过了fastjson自动转成了对象
                    if (data.code === 0) {
                        /* 调用顶层窗口的跳转方法 父窗口：window.parent */
                        window.top.location.href = 'javascript:history.go(-1)';
                        window.top.alert("更新成功！");
                    } else {
                        layer.alert('修改失败！');
                    }
                },
                error: function () {
                    layer.alert("修改失败！");
                }
            });
        }

        function goBack() {
            window.top.location.href = 'javascript:history.go(-1)';
        }
    </script>

    <script src="layui/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
    <script>
        layui.use(['layer', 'form'], function () {
            var form = layui.form,
                layer = layui.layer;

            initForm(form);
            //监听提交
            form.on('submit(demo1)', function (data) {
                update(data);
            });

        });
    </script>
</head>

<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>更新用户信息</legend>
</fieldset>

<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">ID</label>
        <div class="layui-input-block">
            <input type="text" name="userId" disabled="true" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">联系电话</label>
        <div class="layui-input-block">
            <input type="text" name="userMobile" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="userSex" value="男" title="男">
            <input type="radio" name="userSex" value="女" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1" onclick="return false">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="goBack()">返回</button>
        </div>
    </div>
</form>
</body>
</html>
